package com.sbl.bajw.pc.service;

import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.BaiduMsgPush;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.MsgBoxDao;
import com.sbl.bajw.pc.model.MsgBoxModel;
import com.sbl.bajw.util.LogUtil;

@Service
public class MsgBoxService {
	
	@Resource
	private MsgBoxDao dao;
	
	private static Logger log;
	
	@Resource
	private MyBatisDao gridDao;
	
	static{
		log = LogUtil.getLogger("msgboxdao");
	}
	
	public void insert(MsgBoxModel model) throws Exception{
		try {
			String useridsStr = RequestUnit.getString("userids");
			String isToAll = RequestUnit.getString("isToAll");
			model.setCreater(SessionUnit.getUserId());
			dao.insert(model);
			if(isToAll != null && !isToAll.equals("")){
				BaiduMsgPush.PushToAll(model.getContent(), model.getTitle(), "{\"todo\":\"msg\"}");
			}
			else{
				String[] userids = useridsStr.split(",");
				
				for(int i=0;i<userids.length;i++){
					String[] userInfos = userids[i].split("@");
					if(userInfos.length < 3){
						continue;
					}
					String baiduChannelid = userInfos[1];
					String baiduUserid = userInfos[2];
					BaiduMsgPush.msgPush(baiduUserid, baiduChannelid, model.getContent(), model.getTitle(), "{\"todo\":\"msg\"}");
				}
			}
			
		} catch (Exception e) {
			log.info("发送失败", e);
			throw e;
		}
	}
	
	public String getList(){
		String title = RequestUnit.getString("title");
		String content = RequestUnit.getString("content");
		String creater = RequestUnit.getString("creater");
		String tuser = RequestUnit.getString("tuser");
		String readed = RequestUnit.getString("readed");
		String startDate = RequestUnit.getString("startDate");
		String stopDate = RequestUnit.getString("stopDate");
		
		HashMap<String,Object> parm = new HashMap<String, Object>();
		if(title != null && !title.equals("")){
			parm.put("title", "%"+title+"%");
		}
		
		if(content != null && !content.equals("")){
			parm.put("content", "%"+content+"%");
		}
		
		if(creater != null && !creater.equals("")){
			parm.put("creater", "%"+creater+"%");
		}
		
		if(tuser != null && !tuser.equals("")){
			parm.put("tuser", "%"+tuser+"%");
		}
		
		parm.put("readed", readed);
		parm.put("startDate", startDate);
		parm.put("stopDate", stopDate);
		
		String listJson = gridDao.selectPage(MsgBoxDao.class, parm, "getList");
		return listJson;
	}
	
	public MsgBoxModel getDetail(){
		String id = RequestUnit.getString("id");
		MsgBoxModel model1 = new MsgBoxModel();
		model1.setId(id);
		model1.setReaded("1");
		dao.update(model1);
		MsgBoxModel model = dao.getDetail(id);
		return model;
	}
	
	public String getListByuser(){
		String userid = RequestUnit.getString("user_id");
		
		HashMap<String,Object> parm = new HashMap<String, Object>();
		parm.put("userid", userid);
		
		String listJson = gridDao.selectPage(MsgBoxDao.class, parm, "getListByUser");
		return listJson;
	}
	
	public void delete(){
		String id = RequestUnit.getString("id");
		dao.delete(id);
	}
}
